from datetime import datetime

from sqlalchemy import Column, Text
from sqlmodel import Index, SQLModel, Field


class Book(SQLModel ,table=True):
    __tablename__:str = "book"
    id: int = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    author: str = Field(index=True)
    cover: str = Field(default="")
    description: str = Field(
        default="",
        sa_column=Column(Text)
    )
    category: str = Field(default="")
    tags: str = Field(default="")
    total_chapter: int = Field(default=0)
    created_at: datetime = Field(default_factory=datetime.now)
    words_cnt:int = Field(default=0)
    is_ended:bool = Field(default=False)
    __table_args__ = (
        Index("index_author", "author"),
        Index("index_name", "name"),
        Index("idx_category", "category"),
        Index("idx_is_ended", "is_ended"),
        Index("idx_words_cnt", "words_cnt"),
        Index("idx_category_is_ended", "category", "is_ended"),
        Index("idx_category_words", "category", "words_cnt"),
    )
    
    @classmethod
    def sort_fields(cls):
        return [
            "id",
            "name",
            "author",
            "total_chapter",
            "created_at",
            "words_cnt",
            "is_ended",
        ]



